package com.gitee.wsl.common.chart.animation

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import com.gitee.wsl.common.ui.api.InvalidateAble

abstract class SampleChartDataAnimation<T>: AbstractChartAnimation<T>() {

    override fun animateFrom(
        startPosition: Float,
        entries: List<T>,
        callback: com.gitee.wsl.common.ui.api.InvalidateAble
    ): AbstractChartAnimation<T> {
        val propName = getObjectPropName()
        // Entries animators
        entries.forEach { dataPoint ->
            val eAnimator: ObjectAnimator =
                ObjectAnimator.ofFloat(
                    dataPoint,
                    propName,
                    startPosition,
                    getObjectPropValue(dataPoint)
                )
            eAnimator.duration = duration
            eAnimator.interpolator = interpolator
            eAnimator.start()
        }

        // Global animator
        val animator: ValueAnimator = ValueAnimator.ofInt(0, 1)
        animator.addUpdateListener { callback.invalidate() }
        animator.duration = duration
        animator.interpolator = interpolator
        animator.start()

        return this
    }

    abstract fun getObjectPropName():String

    abstract fun getObjectPropValue(item:T):Float

}